package com.yueyang.sojourn.server.controller.wx;

import com.yueyang.sojourn.server.common.LoginNeedless;
import com.yueyang.sojourn.server.common.consts.Constants;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.controller.model.JsonResult;
import com.yueyang.sojourn.server.model.enums.MemberStatus;
import com.yueyang.sojourn.server.model.po.Member;
import com.yueyang.sojourn.server.model.vo.SojournUserVo;
import com.yueyang.sojourn.server.service.member.MemberService;
import com.yueyang.sojourn.server.service.share.ShareDistributorService;
import com.yueyang.uc.model.UcUserV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Stephen.Shi
 * @version v1
 * @summary 用户信息
 * @since 2020/2/26
 */
@RestController
@RequestMapping("/api/wx/user")
@Api(tags = "(小程序)用户信息 API")
@Slf4j
public class ApiUserController extends BaseController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private ShareDistributorService distributorService;

    @ApiOperation(value="获取用户信息", notes="获取用户的账号、昵称、头像、会员等信息，用于显示。 不要直接显示微信的头像和昵称。")
    @GetMapping("/info")
    @LoginNeedless
    public JsonResult<SojournUserVo> user(@RequestHeader(Constants.LOGIN_TOKEN) String token) {
        try {
            SojournUserVo sojournUserVo = new SojournUserVo();
            UcUserV1 ucUserV1 = getLoginUser();
            BeanUtils.copyProperties(ucUserV1, sojournUserVo);
            sojournUserVo.setUserId(ucUserV1.getId());
            memberService.fillMemberInfo(sojournUserVo);
            sojournUserVo.setDistributor(this.distributorService.isDistributor(ucUserV1.getId()));
            return ok().of(sojournUserVo);
        } catch (Exception e) {
            log.info("未登录");
            return fail(-1, "未登录");
        }
    }

}
